<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$basePath = FCPATH.'app/helpers/';
require_once($basePath.'iloader_helper.php');
require_once($basePath.'loadermodel_helper.php');
require_once($basePath.'loader_helper.php');

session_start();

class Loaderform extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        /*$this->load->helper('iloader');
        $this->load->helper('loadermodel');
        $this->load->helper('loader');*/
    }

    private function getLoader()
    {
        //$loader = $this->session->userdata('loader');
        $loader = $_SESSION['loader'];
        if($loader === null || $loader == "")
        {
            $loader = new Loader();
            //$this->session->set_userdata('loader', $loader);
            $_SESSION['loader'] = $loader;
        }
        return $loader;
    }

    private $Records = null;
    public function load($fileName = "TM Scenario 130621X2.csv")
    {
        $loader = $this->getLoader();
        $fileName = FCPATH.'/assets/'.$fileName;
        $file = fopen($fileName, "r");
        $fileContent = fread($file, filesize($fileName));
        fclose($file);
        $excelContent = explode("\r\n", $fileContent);
        $columns = array();
        $records = array();
        for($i=0; $i<sizeof($excelContent); $i++)
        {
            if($i == 0)
            {
                //$columns = explode(",", $excelContent[$i]);
                $columns = explode(",", $excelContent[$i]);
                for($j=0; $j<sizeof($columns); $j++)
                    $columns[$j] = trim($columns[$j]);
                continue;
            }

            $row = explode(",", trim($excelContent[$i]));
            if($row[0] === "")continue;

            $record = array();
            for($j=0; $j<sizeof($row); $j++)
            {
                if($j >= sizeof($columns)) continue;
                $record[$columns[$j]] = $row[$j];
            }
            $records[] = $record;
            $do = new DeliveryOrder($record['Delivery'], $record['Cust Code'], $record['Street'], $record['Weight'], $record['Volume'], $record['Longitude'], $record['Latitude']);
            $do->insert($this->get_instance()->db);
            $loader->getWarehouse()->addDeliveryOrder($do);
        }
        $this->Records = $records;

        /*$loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder("A", 10, 5, 107.02207546, -6.17767511));
        $loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder("AA", 10, 5, 107.0962145, -6.24928696));
        $loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder("AAA", 10, 5, 107.08754437, -6.25793724));
        $loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder("B",10, 15, 107.09630523, -6.24807987));
        $loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder("BA", 35, 5, 107.09611915, -6.24987126));
        $loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder("C", 5, 6, 107.09611915, -6.24987126));*/
        /*echo sizeof($loader->getWarehouse()->DeliveryOrderList)."<br />".
            sizeof(LoaderForm::getLoader()->getWarehouse()->DeliveryOrderList);*/
    }

    public function generate()
    {
        $loader = $this->getLoader();

        $loader->generate($this->get_instance()->db);

        //return $carList;
        //echo $this->getCarGroupList();
    }

    public function getDeliveryOrderList()
    {
        $loader = $this->getLoader();

        $doList = "[";
        foreach($loader->getWarehouse()->DeliveryOrderList as $index => $val)
        {
            $doList .= $val->toString().", ";
        }
        $doList = trim($doList, ", ");
        $doList .= "]";
        //return $doList;
        echo $doList;
    }

    public function getCarGroupList()
    {
        $loader = $this->getLoader();

        /*echo "<pre>";
        print_r($loader->getWarehouse()->CarList);
        echo "</pre>";*/

        $carGroupList = "[";
        foreach($loader->getWarehouse()->CarList as $index => $val)
        {
            $carGroupList .= $val->toString().", ";
        }
        $carGroupList = trim($carGroupList, ", ");
        $carGroupList .= "]";
        //return $carGroupList;
        echo $carGroupList;
    }

    public function getUndeliveredDO()
    {
        $loader = $this->getLoader();

        $doList = "[";
        foreach($loader->getWarehouse()->getUndeliveredDO() as $index => $val)
        {
            $doList .= $val->toString().", ";
        }
        $doList = trim($doList, ", ");
        $doList .= "]";
        //return $doList;
        echo $doList;
    }

    public function getPathList()
    {
        $loader = $this->getLoader();

        $pathList = "[";
        foreach($loader->getWarehouse()->CarList as $index => $val)
        {
            foreach($val->Path as $idx2 => $val2)
            {
                $obj = "{".
                    "CarID : '".$val->CarID."',".
                    "MaxDistance : '".$val->MaxDistance."',".
                    "MaxWeight : '".$val->MaxWeight."',".
                    "MaxVolume : '".$val->MaxVolume."',".
                    "Weight : '".$val->Weight."',".
                    "Volume : '".$val->Volume."',".
                    "DOID : '".$val2->ID."',".
                    "DONum : '".$val2->DONum."',".
                    "CustCode : '".$val2->CustCode."',".
                    "Distance : '".$val2->Distance."',".
                    "DOWeight : '".$val2->Weight."',".
                    "DOVolume : '".$val2->Volume."',".
                    "Delivered : '".$val2->Delivered."',".
                    "Longitude : '".$val2->getLongitude()."',".
                    "Latitude : '".$val2->getLatitude()."'}";
                $pathList .= $obj.",";
            }
        }
        $pathList = trim($pathList, ", ");
        $pathList .= "]";
        //return $pathList;
        echo $pathList;
    }

    public function reset()
    {
        $loader = $this->getLoader();
        $loader->getWarehouse()->reset();
    }
}
